package com.cloudkinto.controller.statistics;

import com.cloudkinto.base.BaseController;
import com.cloudkinto.common.annotations.UserSessionRequired;
import com.cloudkinto.common.utils.TimeUtils;
import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;
import com.cloudkinto.service.statistics.NotificationService;
import com.cloudkinto.service.statistics.StatisticsService;
import com.cloudkinto.service.statistics.vo.StatisticsSalesReq;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @author zhangyy
 * @date 2020/6/19 9:24
 * @description:
 */
@RestController
@Api(tags = "首页统计模块")
@RequestMapping("/api/census")
public class StatisticsController extends BaseController {

    @Autowired
    private StatisticsService service;
    @Autowired
    private NotificationService notificationService;

    @UserSessionRequired
    @GetMapping("/statistics")
    @ApiOperation(value = "第一栏统计", notes = "统计")
    public SingleResult statistics() {
        Long companyId = getCompanyId();
        Long userId = getCurrentUserId();
        return service.statistics(userId, companyId);
    }

    @UserSessionRequired
    @GetMapping("/getCompanyAccount")
    @ApiOperation(value = "获取公司标识和余额", notes = "统计")
    public SingleResult getCompanyAccount() {
        Long companyId = getCompanyId();
        Long userId = getCurrentUserId();
        return service.getCompanyAccount(userId, companyId);
    }

    @UserSessionRequired
    @GetMapping("/salesListPage")
    @ApiOperation(value = "7天店铺销量排行", notes = "统计")
//    @ApiImplicitParams({@ApiImplicitParam(name = "type", value = "时间类型 1 一周 2 一月 3 一年 4 自定义"),
//            @ApiImplicitParam(name = "startDate", value = "开始日期  yyyy-MM-dd", example = "2021-05-01"),
//            @ApiImplicitParam(name = "endDate", value = "结束日期 yyyy-MM-dd", example = "2021-05-20")})
    public SingleResult salesListPage(HttpServletRequest request) {
//        return SingleResult.success(service.getSalesList(request.getParameter("type"),
//                request.getParameter("startDate"), request.getParameter("endDate"), getShopId(), getCompanyId()));
        return SingleResult.success(service.getSalesList(getShopId(), getCompanyId()));
    }

    @UserSessionRequired
    @GetMapping("/stockListPage")
    @ApiOperation(value = "SKU销量排行榜", notes = "统计")
    @ApiImplicitParams({@ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "pageSize", value = "页大小"),
            @ApiImplicitParam(name = "type", value = "时间类型 1 一周 2 一月 3 一年 4 自定义"),
            @ApiImplicitParam(name = "startDate", value = "开始日期  yyyy-MM-dd", example = "2021-05-01"),
            @ApiImplicitParam(name = "endDate", value = "结束日期 yyyy-MM-dd", example = "2021-05-20")})
    public PageResult stockListPage(HttpServletRequest request) {
        Long companyId = getCompanyId();

        Map<String, String> map = new HashMap<>();
        String current = request.getParameter("current");
        String pageSize = request.getParameter("pageSize");
        String type = request.getParameter("type");

        map.put("companyId", companyId + "");
        map.put("current", current);
        if (getShopId() != null) {
            map.put("shopId", getShopId() + "");
        }
        map.put("pageSize", pageSize);
        if ("1".equals(type)) {
            map.put("startDate", TimeUtils.getCurrentDateFormat(-7));
            map.put("endDate", TimeUtils.getCurrentDateFormat(1));
        } else if ("2".equals(type)) {
            map.put("startDate", TimeUtils.getCurrentDateFormat(-30));
            map.put("endDate", TimeUtils.getCurrentDateFormat(1));
        } else if ("3".equals(type)) {
            map.put("startDate", TimeUtils.getCurrentDateFormat(-365));
            map.put("endDate", TimeUtils.getCurrentDateFormat(1));
        } else if ("4".equals(type)) {
            map.put("startDate", request.getParameter("startDate"));
            map.put("endDate", request.getParameter("endDate"));
        }
        return service.getListPage(map);
    }


    @UserSessionRequired
    @PostMapping("/salesStatisticsPage")
    @ApiOperation(value = "地区和品类销量統計", notes = "统计")
    @ApiImplicitParams({@ApiImplicitParam(name = "type", value = "时间类型 1 一周 2 一月 3 一年 4 自定义"),
            @ApiImplicitParam(name = "startDate", value = "开始日期  yyyy-MM-dd", example = "2021-05-01"),
            @ApiImplicitParam(name = "endDate", value = "结束日期 yyyy-MM-dd", example = "2021-05-20")})
    public SingleResult salesStatisticsPage(@RequestBody StatisticsSalesReq req) {
        Long companyId = getCompanyId();

        Map<String, String> map = new HashMap<>();
        int type = req.getType();
        map.put("companyId", companyId + "");
        if (getShopId() != null) {
            map.put("shopId", getShopId() + "");
        }
        if (1 == type) {
            map.put("startDate", TimeUtils.getCurrentDateFormat(-7));
            map.put("endDate", TimeUtils.getCurrentDateFormat(1));
        } else if (2 == type) {
            map.put("startDate", TimeUtils.getCurrentDateFormat(-30));
            map.put("endDate", TimeUtils.getCurrentDateFormat(1));
        } else if (3 == type) {
            map.put("startDate", TimeUtils.getCurrentDateFormat(-365));
            map.put("endDate", TimeUtils.getCurrentDateFormat(1));
        } else if (4 == type) {
            map.put("startDate", req.getStartDate());
            map.put("endDate", req.getEndDate());
        }
        return service.salesStatisticsPage(map);
    }


    @UserSessionRequired
    @GetMapping("/notificationList")
    @ApiOperation(value = "通告列表", notes = "统计")
    @ApiImplicitParams({@ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "pageSize", value = "页大小")})
    public SingleResult notification(HttpServletRequest request) {
        Long userId = getCurrentUserId();
        Map<String, String> map = new HashMap<>();
        String current = request.getParameter("current");
        String pageSize = request.getParameter("pageSize");
        map.put("current", current);
        map.put("pageSize", pageSize);
        map.put("userId", userId + "");
        return SingleResult.success(notificationService.getListPage(map));
    }


    @UserSessionRequired
    @GetMapping("/notificationNewest")
    @ApiOperation(value = "获取未读的最新通告", notes = "统计")
    public SingleResult notificationNewest() {
        Long userId = getCurrentUserId();
        Map<String, String> map = new HashMap<>();
        map.put("userId", userId + "");
        if (getCompanyId() != null)
            map.put("companyId", getCompanyId() + "");
        return SingleResult.success(notificationService.getListHomePageData(map));
//        return notificationService.notificationNewest(userId);
    }


    @UserSessionRequired
    @GetMapping("/confirmHavingRead")
    @ApiOperation(value = "确认已读", notes = "确认已读")
    public SingleResult confirmHavingRead(@RequestParam Long id) {
        Long userId = getCurrentUserId();
        return notificationService.confirmHavingRead(id, userId);
    }
}
